package org.ediba.siri.core;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class Util {
	
	public static String currentUsername(){
		return SecurityContextHolder.getContext().getAuthentication().getName();
	}
	
	public static boolean currentUserIsAnonymous(){
		return Util.currentUserHasRole("ROLE_ANONYMOUS");
	}
	
	public static boolean currentUserHasRole(String roleName){
		Collection<GrantedAuthority> grantedAuthorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		
		for (GrantedAuthority grantedAuthority : grantedAuthorities)
			if (grantedAuthority.getAuthority().equalsIgnoreCase(roleName))
				return true;
		
		return false;
	}
	
	public static List<Integer> CSV2IntegerList(String csv){
		if (csv == null)
			return null;
		else{
			String[] _csv = csv.split(",");
			List<Integer> _list = new ArrayList<Integer>(_csv.length);
			for (String string : _csv) 
				_list.add(Integer.parseInt(string));
			return _list;
		}
	}
	
	public static List<String> CSV2StringList(String csv){
		if (csv == null)
			return null;
		else{
			String[] _csv = csv.split(",");
			List<String> _list = new ArrayList<String>(_csv.length);
			for (String string : _csv) 
				_list.add(string);
			return _list;
		}
	}

	public static File[] finder( String dirName, final String fileName){
		File dir = new File(dirName);

		return dir.listFiles(new FilenameFilter() { 
			public boolean accept(File dir, String filename){ 
				return filename.startsWith(fileName);
			}
	    });
	}

	public static String getFileName(String fileName){
		int dotIndex = fileName.lastIndexOf('.');
		if (dotIndex == -1)
			return fileName;
		else 
			return fileName.substring(0, dotIndex);
	}
	
	public static String getFileExtension(String fileName){
		int dotIndex = fileName.lastIndexOf('.');
		if (dotIndex == -1)
			return "";
		else 
			return fileName.substring(dotIndex+1, fileName.length());
	}

	public static Map<Filter,Collection<Object>> simpleFilterA(Filter filter,Object value){
		
		Map<Filter,Collection<Object>> _and = new HashMap<Filter,Collection<Object>>(1);
		Collection<Object> _filters = new ArrayList<Object>(1);
		_filters.add(value);
			
		_and.put(filter, _filters);
		
		return _and;
	}
	
	public static Collection<Map<Filter,Object>> simpleFilter(Filter filter,Object value){
		
		Collection<Map<Filter,Object>> _or = new HashSet<Map<Filter,Object>>(1);
		Map<Filter, Object> _filter = new HashMap<Filter, Object>(1);
		
		_filter.put(filter, value);
			
		_or.add(_filter);
		
		return _or;
	}
	
	public static Collection<Map<Filter,Object>> simpleFilterOr(Filter filter, @SuppressWarnings("rawtypes") Collection values){
		
		Collection<Map<Filter,Object>> _or = new HashSet<Map<Filter,Object>>(values.size());
		Map<Filter, Object> _filter;
		
		for (Object value : values) {
			_filter = new HashMap<Filter, Object>(1);
			_filter.put(filter, value);
			_or.add(_filter);
		}
		
		return _or;
	}
	
}
